package com.jinyu.jyly.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jinyu.jyly.R;
import com.jinyu.jyly.adapter.HomeTabAdapter;
import com.jinyu.jyly.base.BaseLazyFragment;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.bean.ArticleListInfo;
import com.jinyu.jyly.ui.home.articledetail.ArticleDetailActivity;
import com.jinyu.jyly.utils.AES;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 *
 * @author 62485
 */
public class HomeTabFragment extends BaseLazyFragment<HomePresenter> implements HomeContract.View, BaseQuickAdapter.OnItemChildClickListener{

    @BindView(R.id.rcv_recycleview)
    RecyclerView rcv_recycleview;
//    @BindView(R.id.refreshLayout)
//    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.cl_empty)
    ConstraintLayout rl_empty;
    private boolean hasLoadMore = false;
    private boolean isFristLoad = true;
    private int PAGE_NO = 1;
    private int PAGE_SIZE = 10;
    private HomePresenter mPresenter;


    public static HomeTabFragment newInstance(String type) {
        HomeTabFragment newFragment = new HomeTabFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        newFragment.setArguments(bundle);
        return newFragment;
    }


    @Override
    protected View initView(ViewGroup container) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_home_tab, container, false);
        return view;
    }

    @Override
    protected void loadData() {
        initIntent();
        initRecycleview();
    }

    private void initIntent() {
        Bundle bundle = getArguments();
        String type = bundle.getString("type");
        if("1".equals(type)){
            //资讯
            try {
                mTipDialog.show();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("cid", 39);
//                jsonObject.put("tui", 1);
                String pamam = AES.encryptToHexString(jsonObject.toString());
                mPresenter.getSiteArticle(pamam);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }else{
            //常见问题
            try {
                mTipDialog.show();
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("cid", 44);
//                jsonObject.put("tui", 1);
                String pamam = AES.encryptToHexString(jsonObject.toString());
                mPresenter.getSiteArticle(pamam);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    private void initRecycleview() {
        rcv_recycleview.setNestedScrollingEnabled(false);
        rcv_recycleview.setLayoutManager(new LinearLayoutManager(getActivity()));
//        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
//                PAGE_NO = 1;
//                mPresenter.getCollectFollowList(PAGE_NO+"",PAGE_SIZE+"");
//                hasLoadMore = false;
//            }
//        });
//
//        mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
//            @Override
//            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
//                ++PAGE_NO;
//                mPresenter.getCollectFollowList(PAGE_NO+"",PAGE_SIZE+"");
//                hasLoadMore = true;
//            }
//        });


    }

    @Override
    public HomePresenter getPresenter() {
        if(mPresenter == null){
            mPresenter = new HomePresenter(getActivity(), this);
        }
        return mPresenter;
    }


    @Override
    public void onFailer(Throwable throwable) {
        mTipDialog.dismiss();
//        mRefreshLayout.finishLoadMore();
//        mRefreshLayout.finishRefresh();
//        mRefreshLayout.resetNoMoreData();
    }

    @Override
    public void onSiteAd(AllInfoBean info) {
        //空实现
    }

    //空实现
    @Override
    public void onSiteGongGaoArticle(AllInfoBean infoBean) {
        mTipDialog.dismiss();
    }

    /**
     * 获取文章列表
     * @param infoBean
     */
    @Override
    public void onSiteArticle(AllInfoBean infoBean) {
        mTipDialog.dismiss();
        switch (infoBean.getStatus()){
            case 200:
                String beanData = infoBean.getData();
                try {
                    String result = AES.decrypt(beanData);
                    Gson gson = new Gson();
                    Type listType = new TypeToken<ArrayList<ArticleListInfo>>() {}.getType();
                    List<ArticleListInfo> mList = gson.fromJson(result, listType);
                    if(mList != null && mList.size() != 0){
                        rl_empty.setVisibility(View.GONE);
                        rcv_recycleview.setVisibility(View.VISIBLE);
                        HomeTabAdapter articleListAdapter = new HomeTabAdapter(mList, R.layout.item_home_tab);
                        rcv_recycleview.setAdapter(articleListAdapter);
                        articleListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                ArticleListInfo articleListInfo = mList.get(position);
                                if(articleListInfo != null){
                                    int id = articleListInfo.getId();
                                    Intent mDetailIntent = new Intent(getActivity(), ArticleDetailActivity.class);
                                    mDetailIntent.putExtra("id",id);
                                    startActivity(mDetailIntent);
                                }
                            }
                        });
                    }else{
                        rl_empty.setVisibility(View.VISIBLE);
                        rcv_recycleview.setVisibility(View.GONE);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(infoBean.getMsg());
                break;
            default:break;
        }
    }



    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

    }
}
